package com.example.aircraft.gamemode;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.example.aircraft.GamePanel;
import com.example.aircraft.MyApplication;
import com.example.aircraft.aircraft.Enemy;
import com.example.aircraft.database.User_Data;
import com.example.aircraft.panel.ChoosePattern;
import com.example.aircraft.panel.DualSignPanel;

import java.io.IOException;

public class DualGame extends NormGame{
    private static int opScore;
    private static String opName;
    private Context context;
    public DualGame(Context context) {
        super(context);
        this.context = context;
        user_data = DualSignPanel.getUser_data();
    }
    @Override
    public void run(){
        while(this.isValid) {
            draw_bg();
            if (timeCountAndNewCycleJudge()) {
                // 新敌机产生
                if (enemyAircrafts.size() < enemyMaxNumber) {
                    Enemy enemy = enemyFactory.setEnemy(this.score);
                    enemyAircrafts.add(enemy);
                    enemy.registerObject(enemyManager);
                }
                // 飞机射出子弹,试图用状态机
            enemyshootAction();
            }
            if (timeCountAndNewCycleJudge_Hero()) {
                //英雄机射出子弹
                heroshootAction();
            }
            // 子弹移动
            bulletsMoveAction();
            // 飞机移动
            aircraftsMoveAction();
            //补给移动
            propsMoveAction();
            // 撞击检测
            crashCheckAction();
            // 后处理
            postProcessAction();
            if(hero.notValid()){

                        Log.i("client", "send message to server");
                        MyApplication.getWriter().println("1919_810#"+user_data.getUSER_NAME()+","+score);
                        MyApplication.getWriter().flush();
                        String content = "";
                        while (true) {
                            try {
                                if ((content = MyApplication.getIn().readLine()) != null)
                                    break;
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        String[] command = content.split("#|,");
                        if ("1919_810".equals(command[0])){// 实际情况时比对和自己的用户名是否相同
                            Log.d("REGTEST", content);
                            opScore = Integer.parseInt(command[2]);
                            opName = command[1];
                            MyApplication.getWriter().println("1919_810#"+user_data.getUSER_NAME()+","+score);// 再发一遍
                            MyApplication.getWriter().flush();
                            Intent intent  = new Intent(context, GamePanel.class);
                            context.startActivity(intent);
                        }



                Log.d("BattleTest","对战结束！");

            }

            try {
                Thread.sleep(timeIntervaL_Hero);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static User_Data getUser_data(){
        return user_data;
    }
    public static int getOpScore(){
        return opScore;
    }

    public static String getOpName() {
        return opName;
    }
    public static int getScore(){
        return score;
    }
}
